package com.classroommanagesystem.controller;


import com.classroommanagesystem.common.Result;
import com.classroommanagesystem.domain.dto.LoginDTO;
import com.classroommanagesystem.domain.dto.RegisterDTO;
import com.classroommanagesystem.domain.dto.UpdatePasswordDTO;
import com.classroommanagesystem.domain.dto.UpdateUserDTO;
import com.classroommanagesystem.domain.entity.User;
import com.classroommanagesystem.service.UserService;


import com.classroommanagesystem.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author author
 * @since 2025-10-24
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登出
     */
    @PostMapping("/logout")
    @Operation(summary = "用户登出")
    public Result logout() {
        userService.logout();
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param dto
     * @return
     */
    @PostMapping("/updatePassword")
    @Operation(summary = "修改密码")
    public Result updatePassword(@RequestBody UpdatePasswordDTO dto) {
        userService.updatePassword(dto);
        return Result.success();
    }



    //  修改信息
    @PostMapping("/updateUserInfo")
    @Operation(summary = "修改信息")
    public Result update(@RequestBody UpdateUserDTO dto) {
        userService.updateUserInfo(dto);
        return Result.success();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取用户信息")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")  // hasRole('ADMIN')也行
    public Result<User> getUserInfo() {
        User user = userService.getById(ThreadLocalUtil.getCurrentId());
        return Result.success("查询成功", user);
    }



}
